﻿module game.battle.projectiles.projectilescontroller;

import gamelib.graphics.renderer2d.renderer;

import game.transformmapper;

struct ProjectilesController
{
    void spawn(T, T1)(ref T container, in T1 projectile) const
    {
        foreach(ref c; container.tupleof)
        {
            import std.traits;
            alias PT = Unqual!(typeof(c[0]));
            static if(is(Unqual!T1 : PT))
            {
                c ~= projectile;
                return;
            }
        }
        assert(false);
    }

    void update(T)(ref T container) const
    {
        foreach(ref c; container.tupleof)
        {
            auto len = c.length;
            for(int i = c.length - 1; i >= 0; --i)
            {
                auto p = c[i];
                p.update();
                if(!p.isAlive)
                {
                    --len;
                    c[i] = c[len];
                }
                else
                {
                    c[i] = p;
                }
            }
            c.length = len;
        }
    }

    void draw(T,T1)(ref T container, ref T1 renderers, Renderer ren, in TransformMapper mapper)
    {
        static assert(container.tupleof.length == renderers.tupleof.length);
        foreach(i,ref c; container.tupleof)
        {
            (renderers.tupleof[i]).draw(ren,mapper,c[0..$]);
        }
    }
}

